<!DOCTYPE html><html lang="[&quot;zh-Hans&quot;,&quot;zh-CN&quot;,&quot;default&quot;]"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="Electron入门笔记"><meta name="keywords" content="electron"><meta name="author" content="wenmu"><meta name="copyright" content="wenmu"><title>Electron入门笔记 | 温木的博客</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="http://ta.qq.com"><script>(function() {
   var hm = document.createElement("script");
   hm.src = "https://tajs.qq.com/stats?sId=&lt;script type=&quot;text/javascript&quot; src=&quot;http://tajs.qq.com/stats?sId=66523541&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt;";
   var s = document.getElementsByTagName("script")[0];
   s.parentNode.insertBefore(hm, s);
 })();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script><meta name="generator" content="Hexo 4.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#进程和线程"><span class="toc-number">1.</span> <span class="toc-text">进程和线程</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#它们的区别"><span class="toc-number">1.1.</span> <span class="toc-text">它们的区别</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Electron、Chromium-和进程之间的关系"><span class="toc-number">2.</span> <span class="toc-text">Electron、Chromium 和进程之间的关系</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Chromium-主进程的特点"><span class="toc-number">2.1.</span> <span class="toc-text">Chromium 主进程的特点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#渲染进程-Render-Process-的特点"><span class="toc-number">2.2.</span> <span class="toc-text">渲染进程 - Render Process 的特点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#主进程和渲染进程可访问-Electron-API-的范围"><span class="toc-number">2.3.</span> <span class="toc-text">主进程和渲染进程可访问 Electron API 的范围</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#BrowserWindow"><span class="toc-number">3.</span> <span class="toc-text">BrowserWindow</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#使用-nodemon-帮助开发"><span class="toc-number">3.1.</span> <span class="toc-text">使用 nodemon 帮助开发</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#进程之间的通讯方式"><span class="toc-number">4.</span> <span class="toc-text">进程之间的通讯方式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#devtron-插件"><span class="toc-number">4.1.</span> <span class="toc-text">devtron 插件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#进程之间进行通讯"><span class="toc-number">4.2.</span> <span class="toc-text">进程之间进行通讯</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#remote-进程之间简单的通讯方式"><span class="toc-number">4.3.</span> <span class="toc-text">remote 进程之间简单的通讯方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#npx"><span class="toc-number">4.4.</span> <span class="toc-text">npx</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#electrong-需要注意的地方"><span class="toc-number">4.5.</span> <span class="toc-text">electrong 需要注意的地方</span></a></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.loli.net/2020/02/27/xwaBoOj158MfNyq.jpg"></div><div class="author-info__name text-center">wenmu</div><div class="author-info__description text-center">记录一些在工作中或学习新知识时学到的一些知识点；正所谓好记性不如一个烂笔头，温故而知新！</div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">115</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">52</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">30</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">温木的博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span></div><div id="post-info"><div id="post-title">Electron入门笔记</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-01-08</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/Electron/">Electron</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>讲解 electron 的一些入门知识。</p>
<a id="more"></a>

<h2 id="进程和线程"><a href="#进程和线程" class="headerlink" title="进程和线程"></a>进程和线程</h2><p>进程和线程就像车间和车间中各个车间的关系。线程是进程的一部分。</p>
<h3 id="它们的区别"><a href="#它们的区别" class="headerlink" title="它们的区别"></a>它们的区别</h3><ol>
<li>内存<br>计算机会给每个进程分配单独的内存空间，通常情况下进程之间不共享内存；线程在进程内部共享进程的内存。</li>
<li>通信机制<br>通常情况下进程之间是不可互通的，通过一些手段也可实现，比如 IPC，但是线程之间通信就很方便快捷</li>
<li>量级的区别<br>线程更轻量级，创建更快，使用资源更少</li>
</ol>
<h2 id="Electron、Chromium-和进程之间的关系"><a href="#Electron、Chromium-和进程之间的关系" class="headerlink" title="Electron、Chromium 和进程之间的关系"></a>Electron、Chromium 和进程之间的关系</h2><p>Chromium 是由一个主进程和多个渲染进程组成，和浏览器一样，浏览器窗口就是主进程(即使打开多个窗口也是一个主进程)，每个 tab 就是一个渲染进程，主进程就是一个指挥官，负责对每个渲染进程进行控制和分配。<br>Electron 也一样，也是一个主进程和多个子进程的关系。</p>
<h3 id="Chromium-主进程的特点"><a href="#Chromium-主进程的特点" class="headerlink" title="Chromium 主进程的特点"></a>Chromium 主进程的特点</h3><ol>
<li>可以使用和系统对接的 Electron API - 比如创建菜单、上传文件等等。</li>
<li>负责创建渲染进程 - Render Process</li>
<li>全面支持 Node.js 语法</li>
<li>只有一个，作为整个程序的入口点</li>
</ol>
<h3 id="渲染进程-Render-Process-的特点"><a href="#渲染进程-Render-Process-的特点" class="headerlink" title="渲染进程 - Render Process 的特点"></a>渲染进程 - Render Process 的特点</h3><ol>
<li>可以有多个，每个对应一个窗口</li>
<li>每个都是一个单独的进程</li>
<li>全面支持 Node.js 和 DOM API</li>
<li>可以使用一部分 Electron 提供的 API</li>
</ol>
<h3 id="主进程和渲染进程可访问-Electron-API-的范围"><a href="#主进程和渲染进程可访问-Electron-API-的范围" class="headerlink" title="主进程和渲染进程可访问 Electron API 的范围"></a>主进程和渲染进程可访问 Electron API 的范围</h3><p><img src="https://upload-images.jianshu.io/upload_images/1348523-d3b387de10300a0b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="主进程和渲染进程可访问Electron API的范围"></p>
<h2 id="BrowserWindow"><a href="#BrowserWindow" class="headerlink" title="BrowserWindow"></a>BrowserWindow</h2><p>创建一个窗口，也即使一个 tab，一个渲染进程- Render Process.</p>
<h3 id="使用-nodemon-帮助开发"><a href="#使用-nodemon-帮助开发" class="headerlink" title="使用 nodemon 帮助开发"></a>使用 nodemon 帮助开发</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">“start”:&quot;nodemon --watch  main.js --exec \&quot;electron .\&quot;&quot;</span><br></pre></td></tr></table></figure>

<h2 id="进程之间的通讯方式"><a href="#进程之间的通讯方式" class="headerlink" title="进程之间的通讯方式"></a>进程之间的通讯方式</h2><ul>
<li>Electron 使用 IPC (interprocess communication) 在进程之间进行通讯</li>
</ul>
<p>进程之间为什么要通讯？<br>因为主进程和渲染进程可访问的资源不同，比如渲染进程需要通过主进程访问一些系统资源等等。</p>
<h3 id="devtron-插件"><a href="#devtron-插件" class="headerlink" title="devtron 插件"></a>devtron 插件</h3><p>使用这个插件可以方便我们调试 electron 程序。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 引入</span><br><span class="line">require(&quot;devtron&quot;).install();</span><br><span class="line"></span><br><span class="line">.....</span><br><span class="line"></span><br><span class="line">win.loadFile(&quot;index.html&quot;);</span><br><span class="line">&#x2F;&#x2F; 自动打开devtron工具</span><br><span class="line">win.webContents.openDevTools();</span><br></pre></td></tr></table></figure>

<h3 id="进程之间进行通讯"><a href="#进程之间进行通讯" class="headerlink" title="进程之间进行通讯"></a>进程之间进行通讯</h3><p>在主进程中注册监听事件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; app, BrowserWindow, ipcMain &#125; = <span class="built_in">require</span>(<span class="string">"electron"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * message 自定义的事件名称</span></span><br><span class="line"><span class="comment"> * event 事件对象</span></span><br><span class="line"><span class="comment"> * arg 事件参数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">ipcMain.on(<span class="string">"message"</span>, (event, arg) =&gt; &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(arg);</span><br><span class="line">  <span class="comment">// 主进程回复渲染进程信息,事件名reply可自定义</span></span><br><span class="line">  event.reply(<span class="string">"reply"</span>, <span class="string">"this is a message from main process."</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>渲染进程中注册和监听事件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; ipcRenderer &#125; = <span class="built_in">require</span>(<span class="string">"electron"</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 发送信息</span></span><br><span class="line">ipcRenderer.send(<span class="string">"message"</span>, <span class="string">"this is a message from render process."</span>);</span><br><span class="line"></span><br><span class="line">监听主进程回复的信息;</span><br><span class="line">ipcRenderer.on(<span class="string">"reply"</span>, (event, arg) =&gt; &#123;</span><br><span class="line">  <span class="built_in">document</span>.getElementById(<span class="string">"main_msg"</span>).innerHTML = arg;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h3 id="remote-进程之间简单的通讯方式"><a href="#remote-进程之间简单的通讯方式" class="headerlink" title="remote 进程之间简单的通讯方式"></a>remote 进程之间简单的通讯方式</h3><p>在渲染进程的 js 中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const &#123; BrowserWindow &#125; &#x3D; require(&quot;electron&quot;).remote;</span><br><span class="line"></span><br><span class="line">let win &#x3D; new BrowserWindow(&#123; height: 400, width: 400 &#125;);</span><br><span class="line">win.loadURL(&quot;https:&#x2F;&#x2F;www.baidu.com&quot;);</span><br></pre></td></tr></table></figure>

<h3 id="npx"><a href="#npx" class="headerlink" title="npx"></a>npx</h3><ul>
<li>避免全局安装</li>
<li>可以使用已安装模块</li>
</ul>
<h3 id="electrong-需要注意的地方"><a href="#electrong-需要注意的地方" class="headerlink" title="electrong 需要注意的地方"></a>electrong 需要注意的地方</h3><ol>
<li>package.json 中配置 main 字段 2.在根目录创建 main.js，这里写主进程的代码</li>
</ol>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wenmu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.wangpengpeng.site/2020/01/08/Electron%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/">http://blog.wangpengpeng.site/2020/01/08/Electron%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.wangpengpeng.site">温木的博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/electron/">electron</a></div><div class="post-qr-code"><div class="post-qr-code-item"><img class="post-qr-code__img" src="https://i.loli.net/2020/02/27/hOBzIxFDJMCrmtN.jpg"><div class="post-qr-code__desc">微信打赏</div></div></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/01/09/%E9%A1%B9%E7%9B%AE%E6%80%A7%E8%83%BD%E5%92%8C%E8%A7%84%E8%8C%83%E6%80%BB%E7%BB%93/"><i class="fa fa-chevron-left">  </i><span>项目性能和规范总结</span></a></div><div class="next-post pull-right"><a href="/2020/01/08/%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD/"><span>前端性能</span><i class="fa fa-chevron-right"></i></a></div></nav><div id="vcomment"></div><script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script>var notify = 'false' == true ? true : false;
var verify = 'false' == true ? true : false;
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
  return GUEST_INFO.indexOf(item) > -1
});
guest_info = guest_info.length == 0 ? GUEST_INFO :guest_info;
window.valine = new Valine({
  el:'#vcomment',
  notify:notify,
  verify:verify,
  appId:'U1QqpeQNuBnRsfygHk1GQ30q-gzGzoHsz',
  appKey:'dunDQzEseDzgsKgw4qLQcog1',
  placeholder:'支持markdown！',
  avatar:'wavatar',
  guest_info:guest_info,
  pageSize:'20',
  lang: 'zh-cn'
})</script></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2020 By wenmu</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io" target="_blank" rel="noopener"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody" target="_blank" rel="noopener"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv">该文访问量<span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script></body></html>